package week4.num.ExceptionDemo01.neueduTotal.solar;


import java.awt.*;

/**
 * 太阳系行星类型
 */
public class Planet extends AbstrectSolarObject {
    /**
     * 行星的序号
     */
    String no;
    /**
     * 行星的名字
     */
    String name;
    /**
     * 行星半长轴
     */
    int longAxis;
    /**
     * 行星半短轴
     */
    int shortAxis;
    /**
     * 角度
     */
    double theta;

    double speed;

    /**
     * 是否是小行星
     */
    boolean smallPlanet=false;

    public Planet() {
    }

    public Planet(AbstrectSolarObject center,
                  String no, String name, double au,
                  double e, int t, String key) {


        this.center=center;


        this.img = imageUtil.imgMap.get(key);
        this.width = img.getWidth(null);
        this.height = img.getHeight(null);



        this.no = no;
        this.name = name;
        this.longAxis = getLongAxisByAu(au);
        this.shortAxis = getShortAxisByE(e)/2;
        this.theta = 0.0;


        this.speed = getSpeedByT(t);

        this.x = center.x + center.width/2
                -this.width/2+ longAxis;
        this.y = center.y +center.height/2
                -this.height/2;
    }

    /**
     *
     * @param center
     * @param no
     * @param name
     * @param au
     * @param e
     * @param t
     * @param key 在static静态代码块中完成资源的加载
     * @param smallPlanet
     */
    public Planet(AbstrectSolarObject center,
                  String no, String name, double au, double e, int t, String key,boolean smallPlanet) {
        this(center, no, name, au, e, t, key);

        this.smallPlanet=smallPlanet;
    }


    private int getLongAxisByAu(double au) {
        return (int)(au * Constant.RATE);
    }

    private int getShortAxisByE(double e) {
        return (int)(longAxis * Math.sqrt(1 - Math.pow(e, 2)));
    }
    @Override
    public void move() {
        x = (int) (longAxis * Math.cos(theta)+
                center.x + center.width/2-this.width/2);
        y = (int) (shortAxis * Math.sin(theta)+
                center.y +center.height/2-this.width/2);
        theta += speed;
    }
    /**
     * @param t 行星公转周期
     * @return 单位时间线速度方向的改变量
     */
    private double getSpeedByT(int t) {
        return (365.0 / t * Constant.SPEED);
    }

    //画轨道
    private void drawTrace(Graphics g){

        int x= center.x+ center.width/2-longAxis;
        int y= center.y+ center.height/2-shortAxis;
        int width=2*longAxis;
        int height=2*shortAxis;
        g.setColor(Color.white);
        g.drawOval(x,y,width,height);
        g.drawString(this.name,this.x,this.y);
    }

    /*public void run(){
        while (true){
            move();
        }
    }*/

    @Override
    public void draw(Graphics g) {

        if (!smallPlanet){
            drawTrace(g);
        }

        super.draw(g);
        move();
    }
}
